package com.joke.oss;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.oss.OSSClient;
import com.jdbc.mysql.MysqlLocalConnect;
import com.joke.util.Const;
import com.joke.util.DateUtil;

public class UploadSimple {
	private static final Logger logger = LoggerFactory.getLogger(UploadSimple.class);

	private static final String endpoint = "http://oss-cn-beijing.aliyuncs.com";
	private static final String accessKeyId  = "LTAI9yTwVBf6LLpd";
	private static final String accessKeySecret  = "RqHOLMqDsjQqVl7nYoU7iTHimFgkKB";
	private static final String bucketName = "kyhdcms";
	
	public static boolean confirmOSS() {
		OSSClient ossClient = null;
		try {
			ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		} catch (Exception e) {
			return false;
		}finally {
			if(ossClient!=null) {
				ossClient.shutdown();
			}
		}
		return ossClient==null?false:true;
	}
	
	private static Map<String, String> transFile(String pathName,Map<String, String> map) throws IOException {
		
		File file=new File(pathName);
		String files[]= file.list();
		if(file.isDirectory()){
			for(int i=0;i<files.length;i++){
				if(pathName.endsWith(Const.o)) {
					transFile(pathName+files[i],map);	
				}else {
					transFile(pathName+Const.o+files[i],map);
				}
			}
    	}else {
    		String fileName = file.getName();
    			map.put(fileName, pathName);
    	}
		return map;
	}
	/**
	 * 上传图片/视频到阿里云OSS服务器,
	 * @param prefixPath
	 * @param strdate
	 */
	public static void uploadCommon(String prefixPath,String strdate) {
		Date date = new Date();
		
		date = DateUtil.formatDate(strdate);
		String Y = DateUtil.formatString(date, "yyyy");
		String M = DateUtil.formatString(date, "MM");
		String D = DateUtil.formatString(date, "dd");
		String ymd = Y+"/"+M+"/"+D;
		
		String prefix = Const.ossJokePre+ymd+Const.o;
		
		String path = "";
		if(prefixPath.endsWith(Const.o)) {
			path = prefixPath+ymd;
		}else {
			path = prefixPath+Const.o+ymd;
		}
		
		Map<String, String> map = new HashMap<>();
 		try {
			transFile(path,map);
		} catch (IOException e) {
			logger.info(e.getMessage());
		}
 		
 		logger.info("读取的媒体总数："+map.size());
 		InputStream inputStream = null;
 		MysqlLocalConnect.getConnect();
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    	int index = 0;
 		for (String imageName : map.keySet()) {
 			if(MysqlLocalConnect.queryByDay(imageName, strdate)) {
				try {
					inputStream = new FileInputStream(map.get(imageName));
				} catch (FileNotFoundException e) {
					logger.info(e.getMessage());
				}
				
				if(inputStream!=null) {
					ossClient.putObject(bucketName, prefix+imageName, inputStream);
					index++;
				}
 			}
		}
 		MysqlLocalConnect.close();
		ossClient.shutdown();
 		logger.info("上传成功的媒体总数："+index);
		
	}
	public static void uploadOSS() {
			String strdate = DateUtil.formatString(new Date(), DateUtil.YMD_B);
			String picPath = Const.picPath;
			String hotPath = Const.hotPath;
//			String videoPath = Const.videoPath;
			logger.info("上传OSS日期："+strdate);
			
			logger.info("开始上传<热门图片>："+hotPath);
			uploadCommon(hotPath,strdate);
			
			logger.info("开始上传<趣图图片>："+picPath);
			uploadCommon(picPath,strdate);
			
//			logger.info("开始上传<视频和视频封面>："+videoPath);
//			uploadCommon(videoPath,strdate);
	}
	
	public static void uploadOSSUser() {
		String strdate = DateUtil.formatString(new Date(), DateUtil.YMD_B);
		String userPath = Const.userPath;
		logger.info("上传OSS日期："+strdate);
		
		logger.info("开始上传<用户图片>："+userPath);
		uploadCommon(userPath,strdate);
		
	}

	public static void main(String[] args) {
		uploadOSS();
	}
	
	
	/**
	 * 火车头采集用
	 * 上传图片/视频到阿里云OSS服务器,
	 */
	public static void uploadOSSByLoco() {
	//		String strdate = "2018-03-19";
			String strdate = DateUtil.formatString(new Date(), DateUtil.YMD_B);
			String picPath = Const.picPath;
			String hotPath = Const.hotPath;
			String videoPath = Const.videoPath;
			logger.info("上传日期："+strdate);
			uploadCommonLoco(picPath,strdate);
			logger.info("上传完毕："+picPath);
			uploadCommonLoco(hotPath,strdate);
			logger.info("上传完毕："+hotPath);
			uploadCommonLoco(videoPath,strdate);
			logger.info("上传完毕："+videoPath);
	}
	/**
	 * 火车头采集用
	 * 上传图片/视频到阿里云OSS服务器,
	 * @param prefixPath
	 * @param strdate
	 */
	public static void uploadCommonLoco(String prefixPath,String strdate) {
		Date date = new Date();
		
		date = DateUtil.formatDate(strdate);
		String Y = DateUtil.formatString(date, "yyyy");
		String M = DateUtil.formatString(date, "MM");
		String D = DateUtil.formatString(date, "dd");
		String ymd = Y+"/"+M+"/"+D;
		
		String prefix = Const.ossJokePre+ymd+Const.o;
		
		String path = "";
		if(prefixPath.endsWith(Const.o)) {
			path = prefixPath+ymd;
		}else {
			path = prefixPath+Const.o+ymd;
		}
		
		Map<String, String> map = new HashMap<>();
 		try {
			transFile(path,map);
		} catch (IOException e) {
			logger.info(e.getMessage());
		}
 		
 		logger.info("读取的媒体总数："+map.size());
 		InputStream inputStream = null;
 		MysqlLocalConnect.getConnect();
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    	int index = 0;
 		for (String imageName : map.keySet()) {
 			if(MysqlLocalConnect.queryByDay(imageName, strdate)) {
 				try {
					inputStream = new FileInputStream(map.get(imageName));
				} catch (FileNotFoundException e) {
					logger.info(e.getMessage());
				}
 				
 				if(inputStream!=null) {
 					ossClient.putObject(bucketName, prefix+imageName, inputStream);
 					index++;
 				}
 			}
		}
 		MysqlLocalConnect.close();
		ossClient.shutdown();
 		logger.info("上传成功的媒体总数："+index);
	}
	
}
